package com.Base;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XingYang on 2019/2/2.
 * 功能：统一处理回传字符集
 *
 */
public class UTF8processor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        if (o instanceof StringHttpMessageConverter){
            MediaType media = new MediaType("text","plain", Charset.forName("UTF-8"));
            List<MediaType> list = new ArrayList<MediaType>();
            list.add(media);
            ((StringHttpMessageConverter)o).setSupportedMediaTypes(list);


        }
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {

        return o;
    }
}
